/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package mapDrawer;

import map.Map;
import figures.Figure;
import figures.FigureIterator;
import figures.basic.ImageFigure;
import figures.basic.Poligon;
import geometry.Matrix;
import geometry.MovingTransform;
import geometry.Rectangle;
import screen.GisGraphics;

public class DefaultMapDrawer implements MapDrawer {

	Map m = null;
	Figure highlight=null;
	Figure center =null;
	
	public DefaultMapDrawer(Map map){
		m = map;
	}
	
	public void drawFirstPass(GisGraphics g, Rectangle area, MovingTransform t) {
		Rectangle d = new Rectangle();
		t.screen2map(area, d);
		FigureIterator figs = m.getFigures(d);
		if(figs==null)
			return;
		Figure f;
		while(figs.hasNext()){
			f = figs.next();
			if(f instanceof Poligon)
				((Poligon)f).drawInAsPoliline(g, area, t);
			else
				f.drawIn(g, area, t);
		}
	}

	public void drawSecondPass(GisGraphics g, Rectangle area, MovingTransform t) {
		center =null;
		int centerDist2 = Integer.MAX_VALUE;
		Rectangle d = new Rectangle();
		t.screen2map(area, d);
		FigureIterator figs = m.getFigures(d);
		if(figs==null)
			return;
		Figure f;
		while(figs.hasNext()){
			f = figs.next();
			if(f instanceof Poligon)
				f.drawIn(g, area, t);
		}
		figs = m.getFigures(d);
		while(figs.hasNext()){
			f = figs.next();
			if(f instanceof ImageFigure)
				f.drawIn(g, area, t);
			int dist2 = distance(f, t);
			if(dist2<centerDist2){
				centerDist2 = dist2;
				center = f;
			}
				
		}
		
	}

	private int distance(Figure f, MovingTransform t) {
		long dx = t.mapX2screen(f.xcenter(), f.ycenter())-t.getXCentre();
		long dy = t.mapY2screen(f.xcenter(), f.ycenter())-t.getYCentre();
		dx *=dx;
		dy *=dy;
		return (int)(dx+dy);
	}

	public void drawVolatilePass(GisGraphics g, Rectangle area, MovingTransform t, int frame) {
		Rectangle d = new Rectangle();
		t.screen2map(area, d);
		FigureIterator figs = m.getFigures(d);
		figs = m.getFigures(d);
		if(figs==null)
			return;
		Figure f;
		while(figs.hasNext()){
			f = figs.next();
			if(t.getZoom()>(Matrix.pfshift/2))
				f.drawVolatileIn(g, area, t, f.equals(center) || f.equals(highlight), frame);
		}
	}

	public void setHighlight(Figure f) {
		highlight=f;
	}

	public Figure getCenter() {
		return center;
	}

}
